package com.lzw.controller.student;

import com.lzw.anno.Log;
import com.lzw.pojo.RCR;
import com.lzw.service.student.SRoomS;
import com.lzw.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin(origins = "http://localhost:7070")
@RestController
@RequestMapping("/SRC")
public class SRoomC {

    @Autowired
    private SRoomS roomS;

    //查看换寝记录
    @GetMapping("/me")
    public Map<String, Object> get(@RequestHeader("Authorization") String token){
        Map<String, Object> response = new HashMap<>();

        try {
            //解析令牌
            Claims claims = JwtUtils.parseJWT(token.replace("Bearer ",""));
            String student = (String) claims.get("name");

            //查询当前学生
            System.out.println("学生" + student);
            List<RCR> rcrs = roomS.get(student);

            response.put("success",true);
            response.put("RCRs",rcrs);
        }catch (Exception e){
            response.put("success", false);
            response.put("message","无效令牌");
        }
        return response;
    }

    //申请换寝
    @Log
    @PostMapping("/add")
    public String add(@RequestBody RCR rcr){
        roomS.add(rcr);
        return "申请成功";
    }
}
